package by.grsu.epam.dao.entitiesdao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import by.grsu.epam.dao.GenericDAOImpl;
import by.grsu.epam.dao.entitiesdao.UserDAO;
import by.grsu.epam.domain.user.User;
import by.grsu.epam.domain.user.UserRole;

@Repository
public class UserDAOImpl extends GenericDAOImpl<User, Long> implements UserDAO {
	
	public UserDAOImpl(){
		super(User.class);
	}
	
	@Override
	public User findByUsername(String username) {
		Criteria cr = getSession().createCriteria(User.class);
		cr.add(Restrictions.like("username", username));
        return (User) cr.uniqueResult();
	}

	@Override
	public List<UserRole> findRolesById(Long id) {
		User user = this.findById(id);
		return user.getRoles();
	}
}
